package dao

import (
	"github.com/e421083458/gorm"
	"github.com/gin-gonic/gin"
	"math"
	"strconv"
	"time"
)

type TaskConfig struct {
	Id int `json:"id"`
	Name string `json:"name"`
	Level int `json:"level"`
	Cycle int `json:"cycle"`
	MinPrice float64 `json:"min_price"`
	MaxPrice float64 `json:"max_price"`
	ContractRevenue float64 `json:"contract_revenue"`
	Pig float64 `json:"pig"`
	PayPoints float64 `json:"pay_points"`
	QiangPoints float64 `json:"qiang_points"`
	Img string `json:"img"`
	StartTime string `json:"start_time"`
	EndTime string `json:"end_time"`
	Status int `json:"status"`
	IsOpen int `json:"is_open"`
	Bu int `json:"bu"`
	Class int `json:"class"`
	Sta string `json:"sta"`
	End string `json:"end"`
	Djs int `json:"djs"`
	ContractGet string `json:"contract_get"`
	Yuyue int `json:"yuyue"`
}
type List struct {
	Qidai int `json:"qidai"`
	List1 []TaskConfig `json:"list1"`
	List2 []TaskConfig `json:"list2"`
	List3 []TaskConfig `json:"list3"`
}
type Yuyue struct {
	Id int `json:"id"`
	Uid int `json:"uid"`
	PigId int `json:"pig_id"`
	PayPoints float64 `json:"pay_points"`
	CreateTime string `json:"create_time"`
	EndTime string `json:"end_time"`
	Status int `json:"status"`
	UserSort int `json:"user_sort"`
	CreditScore int `json:"credit_score"`
	BuyType int `json:"buy_type"`
	Allowbuy int `json:"allowbuy"`
}
type PigOrder struct {
	Id int
	OrderNo string
	Uid int
	PigId int
	SellId int
	Status int


}
func (t *TaskConfig) FindShoudSell(c *gin.Context, tx *gorm.DB,pig int)  (*TaskConfig,error) {
	//var list []TaskConfig
	//err := tx.Table("task_config").Where("class = 1 and type>0").Order("min_price asc").Find(&lists1).Error
	//return err
	return nil,nil
}

//列表 []TaskConfig 多条数据
func (t *TaskConfig) FindList(c *gin.Context, tx *gorm.DB)  (*List,error) {
	var lists1 []TaskConfig
	err := tx.Table("task_config").Where("class = 1 and type>0").Order("min_price asc").Find(&lists1).Error
	if err != nil {
		return nil, err
	}
	for key,val:=range lists1{
		lists1[key].MinPrice=math.Round(val.MinPrice)
		lists1[key].MaxPrice=math.Round(val.MaxPrice)
		lists1[key].ContractRevenue=math.Round(val.ContractRevenue)
		lists1[key].Img="http://dbwhmingbiao.com"+val.Img
	}
	var lists2 []TaskConfig
	err2 := tx.Table("task_config").Where("class = 2 and type>0").Order("min_price asc").Find(&lists2).Error
	if err2 != nil {
		return nil, err2
	}
	for key,val:=range lists2{
		lists2[key].MinPrice=math.Round(val.MinPrice)
		lists2[key].MaxPrice=math.Round(val.MaxPrice)
		lists2[key].ContractRevenue=math.Round(val.ContractRevenue)
		lists2[key].Img="http://dbwhmingbiao.com"+val.Img
	}
	var lists3 []TaskConfig
	err3 := tx.Table("task_config").Where("class = 3 and type>0").Order("min_price asc").Find(&lists3).Error
	if err3 != nil {
		return nil, err3
	}
	for key,val:=range lists3{
		lists3[key].MinPrice=math.Round(val.MinPrice)
		lists3[key].MaxPrice=math.Round(val.MaxPrice)
		lists3[key].ContractRevenue=math.Round(val.ContractRevenue)
		lists3[key].Img="http://dbwhmingbiao.com"+val.Img
	}
	list := &List{
		Qidai: 1,
		List1: lists1,
		List2: lists2,
		List3: lists3,
	}
	return list,nil
}
func TimetoS(t string) int64 {
	timeLayout := "2006-1-02 15:04:05"  //转化所需模板
	loc, _ := time.LoadLocation("Local")    //获取时区
	tmp, _ := time.ParseInLocation(timeLayout, t, loc)
	timestamp := tmp.Unix()    //转化为时间戳  这个是
	return timestamp
}
//单个
func (t *TaskConfig) Findbyid(c *gin.Context, tx *gorm.DB,id int)  (*TaskConfig,error) {
	list:=&TaskConfig{}
	err := tx.Table("task_config").Where("id = ?",id).Find(list).Error
	if err != nil {
		return nil, err
	}
	list.MinPrice=math.Round(list.MinPrice)
	list.MaxPrice=math.Round(list.MaxPrice)
	list.ContractRevenue=math.Round(list.ContractRevenue)
	list.Img="http://dbwhmingbiao.com"+list.Img

	//时间处理
	now:=time.Now()
	startTime :=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" "+list.StartTime+":00"
	list.Sta=strconv.FormatInt(TimetoS(startTime),10)
	endTime :=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" "+list.EndTime+":00"
	list.End=strconv.FormatInt(TimetoS(endTime),10)

	//判断当前状态
	nowDate:=strconv.Itoa(int(time.Now().Unix()))
	if nowDate>list.Sta && nowDate<list.End{
		list.Status=1
		list.Djs=0
	}else if nowDate<=list.Sta{
		i,err:=strconv.Atoi(list.Sta)
		if err!=nil{
			return nil, err
		}
		list.Status=0
		list.Djs=i-int(time.Now().Unix())
	}else{
		list.Status=2
		list.Djs=0
	}
	//计算收益
	min:=math.Round(list.ContractRevenue*list.MinPrice/100)
	max:=math.Round(list.ContractRevenue*list.MaxPrice/100)
	list.ContractGet=strconv.FormatInt(int64(min),10)+"-"+strconv.FormatInt(int64(max),10)


	uid:=c.Query("uid")
	dayStart :=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" 00:00:00"
	start :=strconv.FormatInt(TimetoS(dayStart),10)

	dayEnd:=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" 23:59:59"
	end :=strconv.FormatInt(TimetoS(dayEnd),10)

	//fmt.Println(start)
	//fmt.Println(end)
	var Ayu []Yuyue
	tx.Table("yuyue").Where("uid = ? and pig_id=? and create_time >? and create_time<?",uid,id,start,end).Find(&Ayu)
	if len(Ayu)==0{
		list.Yuyue=0
	}else{
		list.Yuyue=1
	}

	return list,nil
}
//判断今日是否抢购
func (t *Yuyue) FindQiang(c *gin.Context, tx *gorm.DB,pig int,uid int)  int {

	now:=time.Now()
	dayStart :=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" 00:00:00"
	start :=strconv.FormatInt(TimetoS(dayStart),10)

	dayEnd:=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" 23:59:59"
	end :=strconv.FormatInt(TimetoS(dayEnd),10)
	var Ayu []Yuyue
	tx.Table("yuyue").Where("uid = ? and pig_id=? and create_time >? and create_time<? and status>0",uid,pig,start,end).Find(&Ayu)
	if len(Ayu)==0{
		return 0
	}else{
		return 1
	}


}

func (t *Yuyue) FindYuyue(c *gin.Context, tx *gorm.DB,pig int,uid int)  int {

	now:=time.Now()
	dayStart :=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" 00:00:00"
	start :=strconv.FormatInt(TimetoS(dayStart),10)

	dayEnd:=strconv.Itoa(now.Year())+"-"+strconv.Itoa(int(now.Month()))+"-"+ strconv.Itoa(now.Day())+" 23:59:59"
	end :=strconv.FormatInt(TimetoS(dayEnd),10)
	var Ayu []Yuyue
	tx.Table("yuyue").Where("uid = ? and pig_id=? and create_time >? and create_time<? and status=0",uid,pig,start,end).Find(&Ayu)
	if len(Ayu)==0{
		return 0
	}else{
		return 1
	}


}
